package com.jblv.frame.common.utils;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.apache.commons.lang3.RandomStringUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title PasswordUtils
 * @projectName jblv-frame
 * @packageName com.jblv.frame.common.utils
 * @description PasswordUtils
 * @date Create at 2020/5/18 23:08
 * @since jdk1.8
 */
public class PasswordUtils {


    /**
     *
     * @return 生成密码需要的盐
     */
    public static String getSalt(){
        return RandomStringUtils.randomAlphabetic(64);
    }

    /**
     *
     * @param salt
     * @param pwdPlain
     * @return sha256
     */
    public static String sha256(String salt,String pwdPlain){

        return Hashing.sha256().newHasher().putString(salt + pwdPlain, Charsets.UTF_8).hash().toString();
    }

    public static void main(String[] args) {
        String salt = getSalt();
        System.out.println("salt:"+salt);
        String password = sha256(salt,"111111");
        System.out.println("password:"+password);
    }

}
